package es.wtestgen.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import es.wtestgen.util.DniDuplicadoException;
import es.wtestgen.util.NombreUsuaException;

public abstract class WTestGenAction extends DispatchAction {
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		if(comprobarAutenticado(mapping,form,request,response)){
			return _execute(mapping, form, request, response);
		}else{
			return mapping.findForward("errorUsuarioNoAutenticado");
		}
		
	}
	
	public abstract ActionForward _execute(ActionMapping mapping, ActionForm form,
											HttpServletRequest request, HttpServletResponse response) throws NombreUsuaException, DniDuplicadoException;   
	
	private boolean comprobarAutenticado(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		boolean autenticado = false;
		HttpSession session = request.getSession();
		if(session.getAttribute("esAlumno") != null && session.getAttribute("esProfesor") != null && session.getAttribute("esAdmin") != null){
			boolean esAlumno = (Boolean)session.getAttribute("esAlumno"); 
			boolean esProfesor = (Boolean)session.getAttribute("esProfesor"); 			
			boolean esAdmin = (Boolean)session.getAttribute("esAdmin"); 			
			if(esAlumno || esProfesor || esAdmin){
				autenticado = true;
			}else{
				autenticado = false;			
			}
			
		}
		return autenticado;
	}

}
